import shutil
import requests
from bs4 import BeautifulSoup
import re
import os
# res =requests.get(url="https://s.10010.com/bj/mobilelist-0-0-0-0-0-0-0-0-305-0-0-p2/")
# # res.encoding="gbk"
# # print(res.text)
# goods_object=BeautifulSoup(res.text,"html.parser")
# good_heder=goods_object.find(name="div",attrs={"id":"goodsList"})
# goods=good_heder.find(name="ul",attrs={"class":"goodsListInfor"})
# goods_li_list=goods.find_all(name="li")
# for good in goods_li_list:
#     mobile_object=good.find(name="p",attrs={"class":"mobileGoodsName"})
#     price_object=good.find(name="p",attrs={"class":"evaluation"})
#     data=mobile_object.find(name="a",attrs={"class":"name"})
#     labal=data.find(name="label",attrs={"class":"cOrange"})
#     new_data=data.text.replace(labal.text,"")
#     pj=good.find(name="p",attrs={"class":"evalNum"})
#     match = re.search(r"\d+",pj.text)
#     pj_num=match.group(0)
#     goodsImg_object=good.find_all(name="a",attrs={"class":"goodsImg"})
#     for img in goodsImg_object:
#         img_object=img.find(name="img",attrs={"class":"lazy"})
#         img_info=img_object.attrs
#         img_url=img_info["data-original"]
#         print(f"img:{img_url}")
#         # print("*"*100)
#
#     # print(new_data)
#
#     # print(img_object.text)
#     ccc=f"{new_data}-{price_object.text.strip()}\n评价个数：{pj_num}"
#     print(ccc)
#     # print('*'*100)
FOLDER_PATH=r"D:\code\pythonProject\test\pic"
def download(name,url):

    if not os.path.exists(FOLDER_PATH):
        os.makedirs(FOLDER_PATH)
    res=requests.get(url=url)
    with open (name,mode="wb") as f:
        f.write(res.content)

def run():
    if os.path.exists(FOLDER_PATH):
        shutil.rmtree(FOLDER_PATH)
    res = requests.get(url="https://s.10010.com/bj/mobilelist-0-0-0-0-0-0-0-0-305-0-0-p2/")
    soup_object=BeautifulSoup(res.text,"html.parser")
    # print(soup_object)
    goods_object_list=soup_object.find_all(name="li",attrs={"class":"goodsLi"})
    # print(goods_object_list)
    file=open("商品信息.txt",mode="w",encoding="utf-8")
    for goods in goods_object_list:
        title=goods.find(name="p",attrs={"class":"mobileGoodsName"}).find(name="a").text
        label=goods.find(name="label",attrs={"class":"cOrange"}).text
        pure_title = title.replace(label, "")
        # print(pure_title)
        price=goods.find(name="label",attrs={"class":"priceD"}).text
        price_number=int(re.findall("￥(\d+)",price)[0])
        # print(pure_title, price)
        comment=goods.find(name="p",attrs={"class":"evalNum"}).text
        comment_number = int(re.findall("已有(\d+)人评价", comment)[0])
        # print(pure_title, price, comment)
        image_url=goods.find(name="img").attrs["data-original"]
        pic_name = image_url.rsplit("=")[1]
        file_name = os.path.join(FOLDER_PATH, pic_name.split("&")[0] + ".jpg")
        download(file_name,image_url)
        print(pure_title, price_number, comment_number, image_url)
        line=f"标题：{pure_title}\t价格：{price_number}\t评论数：{comment_number}\t文件本地路径：{file_name}\t图片网络地址：{image_url}\n"
        file.write(line)


    file.close()


if __name__ == '__main__':
    run()